package as3.component.controls
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	import as3.component.Component;
	
	public class Canvas extends Component
	{
		protected var _bg:DisplayObject;
		
		protected var continer:Sprite;
		protected var maskLayer:Sprite;
		
		private var _continerScrollRect:Rectangle;
		
		protected var alertChange:Boolean = false;
		
		public function Canvas()
		{
			super(true);
		}
		
		public function set bg(dis:DisplayObject):void
		{
			if (dis && _bg != dis) 
			{
				_bg = dis;
				setBackgroundView(_bg);
			}
		}
		
	
		override protected function initCom():void
		{
			super.initCom();
			//_uiLibrary["bg"] = "Border5Skin";
			continer = new Sprite();
			maskLayer = new Sprite();
			maskLayer.graphics.beginFill(0,0);
			maskLayer.graphics.drawRect(0,0,4,4);
			maskLayer.graphics.endFill();
			maskLayer.x = maskLayer.y = 0;
			continerScrollRect = new Rectangle(0,0,width, height);
			addChild(continer);
			addChild(maskLayer);
			continer.mask = maskLayer;
			setMasked(true);
		}
		
		override protected function creatUI():void
		{
			super.creatUI();
		}
		
		public function set continerScrollRect(value:Rectangle):void
		{
			_continerScrollRect = value;
			maskLayer.width = _continerScrollRect.width;
			maskLayer.height = _continerScrollRect.height;
			continer.x = 0 - _continerScrollRect.x;
			continer.y = 0- _continerScrollRect.y;
		}
		
		public function get continerScrollRect():Rectangle
		{
			return _continerScrollRect;
		}
		
		public function scrollPos($x:Number, $y:Number):void
		{
			continer.x = 0 - $x;
			continer.y = 0 - $y;
			continer.mask = maskLayer;
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			if (_bg) _bg.width = value;
			_continerScrollRect.width = value;
			continerScrollRect = _continerScrollRect;
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			if (_bg) _bg.height = value;
			_continerScrollRect.height = value;
			continerScrollRect = _continerScrollRect;
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			if (child == continer || child == backgroundView || child == maskLayer || child is ScrollBar) return super.addChild(child);
			else {
				continer.addChild(child);
				checkAlert();
				return child;
			}
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			if (child == continer || child == backgroundView || child == maskLayer || child is ScrollBar) return super.addChildAt(child, index);
			else {
				continer.addChildAt(child, index);
				checkAlert();
				return child;
			}
		}
		
		override public function contains(child:DisplayObject):Boolean
		{
			if (child == continer || child == backgroundView || child == maskLayer || child is ScrollBar) return super.contains(child);
			else return continer.contains(child);
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			if (child == continer || child == backgroundView || child == maskLayer || child is ScrollBar) return super.removeChild(child);
			else {
				continer.removeChild(child);
				checkAlert();
				return child;
			}
		}
		
		override public function getChildAt(index:int):DisplayObject
		{
			return continer.getChildAt(index);
		}
		
		override public function get numChildren():int
		{
			return continer.numChildren;
		}
		
		private function checkAlert():void
		{
			if (alertChange) continerChange();
		}
		
		public function continerChange():void
		{
			
		}
		
		public function get continerBox():Sprite
		{
			return continer;
		}
		
		public function get continerHeight():Number
		{
			var r:Rectangle = continer.getBounds(this);
			if (r.y > 1000000) r.y = 0;
			return r.height + r.y - continer.y;
		}
		
		public function get  continerWidth():Number
		{
			var r:Rectangle = continer.getBounds(this);
			if (r.x > 1000000) r.x = 0;
			return r.width + r.x - continer.x;
		}
	}
}